//
//  MqDetailBrandViewController.m
//  Meiqi
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqDetailBrandViewController.h"
#import "PKRequestManager.h"
#import "MqDetailBrandModel.h"
#import "MqPickModel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "MqDetailBrandTableViewCell.h"
#import "MqFindDetailBrandViewController.h"
#import "MqFindBikeBikeDetailViewController.h"

@interface MqDetailBrandViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic ,strong)UITableView *tableView;

@property (nonatomic ,strong)UILabel *barLabel;

@property (nonatomic ,assign)NSInteger start;

@property (nonatomic ,strong)NSMutableArray *pickArray;

@end

@implementation MqDetailBrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _start = 0;
    //self.view.backgroundColor = [UIColor cyanColor];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = self.name;
    self.tabBarController.tabBar.hidden = YES;
    
    [self addBar];
    [self addButton];
    [self addTableBarLabel];
    [self addTableView];
    [self setUpData];
    

    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self setUpData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _start += 10;
        [self setUpData];
    }];
    
}

- (void)addTableBarLabel
{
    self.barLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenHeight - 40, kScreenWidth, 40)];
    self.barLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_barLabel];
}


//添加bar上的按钮
- (void)addBar
{
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemReply) target:self action:@selector(leftBarClick:)];
    self.navigationItem.leftBarButtonItem = leftBar;
}

- (void)leftBarClick:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;

}
//添加navagationBar下面的button
- (void)addButton
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 50)];
    label.userInteractionEnabled = YES;
    [self.view addSubview:label];
    
    UIButton *choiceButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    choiceButton.frame = CGRectMake(40, 5, (kScreenWidth - 160) / 2, 40);
    //choiceButton.backgroundColor = [UIColor redColor];
    [choiceButton addTarget:self action:@selector(choiceButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [label addSubview:choiceButton];
    
    UIButton *brandDetailButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    brandDetailButton.frame = CGRectMake(120 + (kScreenWidth - 160) / 2, 5, (kScreenWidth - 160) / 2, 40);
    //brandDetailButton.backgroundColor = [UIColor greenColor];
    [brandDetailButton setTitle:@"品牌详情" forState:(UIControlStateNormal)];
    [brandDetailButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [brandDetailButton addTarget:self action:@selector(brandDetailButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [label addSubview:brandDetailButton];
    
}

//  品牌详情按钮的点击方法
- (void)brandDetailButtonClick:(UIButton *)button
{
    MqFindDetailBrandViewController *detailVC = [[MqFindDetailBrandViewController alloc] init];
    detailVC.brand_id = self.brand_id;
    [self.navigationController pushViewController:detailVC animated:NO];
}

//选择按钮的点击方法
- (void)choiceButtonClick:(UIButton *)button
{
    //[self addPickView];
}

//加载数据
- (void)setUpData
{

    NSString *str = [NSString stringWithFormat:@"brand_id=%@&cate_id=%d&start=%ld&num=%d",self.brand_id,0,(long)_start,10];
    NSString *url = [NSString stringWithFormat:@"http://p.biketo.com/api/app/BrandBikes?%@",str];
    
    if(_start == 0)
    {
        _dataArray = [NSMutableArray array];
    }
    
    _pickArray = [NSMutableArray array];
    
    [PKRequestManager requestWithUrlString:url parDic:nil Method:GET finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        
        NSArray *array = dic[@"data"][@"data"];
        for (NSDictionary *oneDic in array) {
            MqDetailBrandModel *model = [[MqDetailBrandModel alloc ]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_dataArray addObject:model];
        }
        
        //给下面的bar赋值
        _barLabel.text = [NSString stringWithFormat:@"共%@款车型",dic[@"data"][@"count"]];
        
        NSArray *arr = dic[@"data"][@"cate_count"];
        for (NSDictionary *countDic in arr) {
            MqPickModel *model = [[MqPickModel alloc ]init];
            [model setValuesForKeysWithDictionary:countDic];
            [_pickArray addObject:model];
        }
        
        
        //停止刷新
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    } path:@"detailbrand"];
    
}
//添加表视图
- (void)addTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50, kScreenWidth, kScreenHeight - (64 + 50 + 40)) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 120;
    [self.view addSubview:_tableView];
}

//添加选择视图
- (void)addPickView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + 50, kScreenWidth, kScreenHeight - (64 + 50))];
    //view.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3];
    
    [self.view addSubview:view];
//    UITouch *touch = [touches anyObject];
//    NSLog(@"%@",touch.view);
    
}


#pragma mark -- tableView代理
//行高
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MqDetailBrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[MqDetailBrandTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"Cell"];
    }
    MqDetailBrandModel *model = _dataArray[indexPath.row];
    cell.model = model;
    return cell;
}


//点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MqFindBikeBikeDetailViewController *bikeDetailVC = [[MqFindBikeBikeDetailViewController alloc] init];
    
    MqDetailBrandModel *model = _dataArray[indexPath.row];
    bikeDetailVC.product_id = model.ID;
    
    bikeDetailVC.brand_id = self.brand_id;
    
   
    
    [self.navigationController pushViewController:bikeDetailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
